<template>
  <div class="login">
    <h3>管理后台登录页面</h3>
    <el-form ref="loginForm" :model="user" :rules="rules">
      <el-form-item prop="username" label="用户名">
        <el-input
          type="text"
          v-model="user.username"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user"
        />
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 100%"
          @click="getLogin('loginForm')"
          >立即登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    getLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('https://www.liulongbin.top:8888/api/private/v1/login',this.user).then(res=>{
              console.log(res)
              if(res.meta.status == 200){
                //   存token 值
                this.$store.commit('stoken',res.data);
                this.$router.go(-1)
                this.$message.success('登录成功')
              }else{
                  this.$message.error(res.meta.msg)
              }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.login {
  width: 30%;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>